/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  SortFile.java                                      
 *  File Contents Outline :   排序      
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package cn.edu.thu.log.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class SortFile {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String fromPath = "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021503/pagesearch_2012021503";
        //String fromPath = "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021508/pagesearch_2012021508";
        //String fromPath = "E:/pangu_new/2.pagesearch-file-20120215-16/pagesearch_20120215.log";
        //String fromPath = "E:/pangu_new/1.pagesearch-ip-20120215-16/collect_temp/pagesearch/10.10.128.13/pagesearch_201202150000_0.log";
        BufferedReader reader = new BufferedReader(new FileReader(new File(
                fromPath)));
        ArrayList<String> all = new ArrayList<String>();
        int originalSize = 0;
        while (reader.readLine() != null) {
            String record = reader.readLine();
            if (record != null) {
                all.add(record);
            }
            originalSize++;
        }
        reader.close();

        Collections.sort(all);
        String toPath = "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021503/pagesearch_2012021503New.log";
        //String toPath = "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021508/pagesearch_2012021508_new1.log";
        //String toPath = "E:/pangu_new/2.pagesearch-file-20120215-16/pagesearch_20120215_New.log";
        //String toPath = "E:/pangu_new/1.pagesearch-ip-20120215-16/collect_temp/pagesearch/10.10.128.13/pagesearch_201202150000_0_new.log";
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
                toPath)));
        int resultSize = 0;
        for (int i = 0; i < all.size(); i++) {
            if (all.get(i) != null) {
                writer.write(all.get(i) + "\n");
                resultSize++;
            }
        }
        writer.close();

        System.out.println("originalSize:" + originalSize);
        System.out.println("resultSize:" + resultSize);
    }
}
